// 封装和文章相关的接口
import { request } from '@/utils'
import { AxiosPromise } from 'axios'

export type Article = {
  comment_count: number
  cover: {
    type: number
    images: string[]
  }
  id: string
  like_count: number
  pubdate: string
  read_count: number
  status: number
  title: string
}
type ArticleRes = {
  data: {
    page: number
    per_page: number
    results: Article[]
    total_count: number
  }
}

/**
 * 获取文章列表数据
 */
export function getArticles(params: any): AxiosPromise<ArticleRes> {
  return request({
    url: '/mp/articles',
    method: 'get',
    params,
  })
}

/**
 * 删除文章接口
 * @param {*} id
 * @returns
 */
export const delArticle = (id: any) => {
  return request.delete(`/mp/articles/${id}`)
}

/**
 * 添加文章
 * @param {*} data
 * @returns
 */
export const addAritcle = (data: any, draft = false) => {
  return request({
    url: `/mp/articles?draft=${draft}`,
    method: 'post',
    data,
  })
}

/**
 * 获取文章详情信息
 * @param {*} id
 * @returns
 */
export const getArticleById = (id: any) => {
  return request.get(`/mp/articles/${id}`)
}

/**
 * 修改文章的接口
 * @param {*} data
 * @param {*} draft
 * @returns
 */
export const updateArticle = (data: any, draft: any) => {
  return request({
    url: `/mp/articles/${data.id}?draft=${draft}`,
    method: 'PUT',
    data,
  })
}
